namespace SimpleWebServices.Models
{
    using System;
    using System.Collections.ObjectModel;
    using System.Xml;
    using System.Xml.Serialization;
    using System.Xml.XPath;
    using SimpleWebServices;
    using Xunit;

    public sealed class EmailCollectionFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<EmailCollection>()
                .DerivesFrom<Collection<Email>>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("emails")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            EmailCollection actual = (
                "<emails>" +
                    "<email key='work' />" +
                    "<email key='fax' />" +
                "</emails>").Deserialize<EmailCollection>();

            Assert.NotNull(actual);

            Assert.NotEmpty(actual);
        }

        [Fact]
        public void deserializeEmpty()
        {
            EmailCollection actual = "<emails />".Deserialize<EmailCollection>();

            Assert.NotNull(actual);

            Assert.Empty(actual);
        }

        [Fact]
        public void serialize()
        {
            EmailCollection obj = new EmailCollection
            {
                new Email()
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("1=count(/emails/email)"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new EmailCollection());
        }

        [Fact]
        public void op_indexer_string()
        {
            EmailCollection obj = new EmailCollection
            {
                new Email
                {
                    Address = "john.doe@example.com",
                    Key = "work"
                },
                new Email
                {
                    Address = "john.doe@example.net",
                    Key = "fax"
                }
            };

            Assert.Same(obj[1], obj["fax"]);
        }

        [Fact]
        public void op_Contains_string()
        {
            EmailCollection obj = new EmailCollection
            {
                new Email
                {
                    Address = "john.doe@example.com",
                    Key = "work"
                }
            };

            Assert.True(obj.Contains("work"));
            Assert.False(obj.Contains("fax"));
        }

        [Fact]
        public void IXmlSerializable_op_GetSchema()
        {
            Assert.Throws<NotSupportedException>(() => (new EmailCollection() as IXmlSerializable).GetSchema());
        }

        [Fact]
        public void IXmlSerializable_op_ReadXml_XmlReaderNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new EmailCollection() as IXmlSerializable).ReadXml(null as XmlReader));
        }

        [Fact]
        public void IXmlSerializable_op_WriteXml_XmlWriterNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new EmailCollection() as IXmlSerializable).WriteXml(null as XmlWriter));
        }
    }
}